<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\FieldTable;
use App\Model\LockSetTable;
use App\Model\NavTable;
use App\Model\AdminTable;
use App\Model\OrderTable;
use App\Model\TimeBlockTable;
use App\Model\WebSetupTable;
use App\Model\ClassTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

//系统管理
class SystemController extends ApiController
{

    /**
     * @api {get} /adminapi/system/stati-stics 控制台-数据统计
     * @apiGroup Admin
     * @apiDescription  说明: 无
     *
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count_orders": 10,
     * "count_user": "2",
     * "count_team": "1"
     * }
     * }
     */
    public function getStatiStics()
    {
        $count_user = \DB::select("select count(*) as c from `user` WHERE status>0")[0]->c;
        $count_team = \DB::select("select count(*) as c from team  WHERE status>0")[0]->c;

        $field_count = FieldTable::ORM()->count();

        $lock_count=LockSetTable::ORM()->where('lock_start_time',Str::getDayTime())->count();
        $all = $field_count * 7-$lock_count; //总






        //查询锁定的

        $count_order = OrderTable::ORM()->whereIn('order_type', [1, 3])->where('pay_type',2)->where('day_time',Str::getDayTime())->count();







        //执行sql
        return JsonRespose::success([
            'count_orders' => round(($count_order/$all)*100,1)."%",// 总订场率
            'count_user' => $count_user,
            'count_team' => $count_team,

        ]);
    }

    //新闻-分类
    public function getClassNewsList()
    {
        $id = 1;
        //勿删
        //$info = WebSetupTable::getWebSetupInfo($id)['data'];
        //$id_category = $info->class_news;
        $id_category = 1;
        $status = 1;
        $class = ClassTable::getClassList($id_category, $status);
        return JsonRespose::done($class);
    }

    //前台部门-分类
    public function getClassDepartmentList()
    {
        $id = 1;
        //勿删
        //$info = WebSetupTable::getWebSetupInfo($id)['data'];
        //$id_category = $info->class_department;
        $id_category = 2;
        $status = 1;
        $class = ClassTable::getClassList($id_category, $status);
        return JsonRespose::done($class);
    }

    //前台门店-分类
    public function getClassStoreList()
    {
        $id = 1;
        //勿删
        //$info = WebSetupTable::getWebSetupInfo($id)['data'];
        //$id_category = $info->class_store;
        $id_category = 6;
        $status = 1;
        $class = ClassTable::getClassList($id_category, $status);
        return JsonRespose::done($class);
    }

    //后台职位-分类
    public function getClassJoinList()
    {
        $id = 1;
        //勿删
        //$info = WebSetupTable::getWebSetupInfo($id)['data'];
        //$id_category = $info->class_admin_join;
        $id_category = 3;
        $status = 1;
        $class = ClassTable::getClassList($id_category, $status);
        return JsonRespose::done($class);
    }

    //网站设置-详情
    public function getWebSetupInfo()
    {
        $id = 1;
        return JsonRespose::done(WebSetupTable::getWebSetupInfo($id));
    }

    //后台导航-添加修改
    public function postWebSetupSave()
    {
        //获取所有参数到$param数组
        $param = \Request::all();
        $data = [
            'id' => $param['id'],
            'title' => $param['title'],
            'img_logo' => $param['img_logo'],
            'img_logo_login' => $param['img_logo_login'],
            'meta_keywords' => $param['meta_keywords'],
            'meta_description' => $param['meta_description'],
            'class_admin_join' => $param['class_admin_join'],
            'class_news' => $param['class_news'],
            'class_department' => $param['class_department'],
            'class_store' => $param['class_store'],
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = WebSetupTable::ORM()->where('id', $param['id'])->update($data);
        } else {
            //否则插入数据
            $res = WebSetupTable::ORM()->insertGetId($data);
            $param['id'] = $res;
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }


    //后台导航-列表
    public function getNavList()
    {
        $status = \Request::get('status', 0);
        return JsonRespose::done(NavTable::getNavList($status));
    }

    //后台导航-添加修改
    public function postNavSave()
    {
        //获取所有参数到$param数组
        $param = \Request::all();
        $data = [
            'id' => $param['id'],
            'id_category' => $param['id_category'],
            'name' => $param['name'],
            'depth' => $param['depth'],
            'url' => $param['url'],
            'icon' => $param['icon'],
            'status' => 1,
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = NavTable::ORM()->where('id', $param['id'])->update($data);
        } else {
            //否则插入数据
            $res = NavTable::ORM()->insertGetId($data);
            //自增id更新为排序id
            NavTable::ORM()->where('id', $res)->update(['sort' => $res]); //排序id
            $param['id'] = $res;
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }

    //后台导航-详情
    public function getNavInfo()
    {
        $id = \Request::get('id');
        return JsonRespose::done(NavTable::getNavInfo($id));
    }

    //后台导航-状态修改
    public function postNavStatus()
    {
        $id = \Request::get('id');
        $status = \Request::get('status');
        //取得当前数据
        $res = NavTable::ORM()->where('id', $id)->update(['status' => $status]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }

    //后台导航-删除
    public function postNavDel()
    {
        $id = \Request::get('id');
        //取得当前数据
        $res = NavTable::ORM()->where('id', $id)->update(['status' => -1]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }

    //后台导航-修改排序
    public function postNavSort()
    {
        $id = \Request::get('id');
        $sort = \Request::get('sort');
        //取得当前数据
        $res = NavTable::ORM()->where('id', $id)->update(['sort' => $sort]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }


    /**
     * @api {post} /adminapi/system/login 登录
     * @apiGroup Admin
     * @apiDescription  说明: 无
     *
     * @apiParam {Str} name --
     * @apiParam {Str} pwd --
     *
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     *    "code": 1,
     *    "msg": "成功",
     *    "data": {}
     *  }
     */
    public function postLogin()
    {
        $name = \Request::get('name');
        $pwd = \Request::get('pwd');
        $res = AdminTable::ORM()->where('name', $name)->first();
        if (!$res) {
            return JsonRespose::fail('用户名不存在');
        }
        if ($pwd === $res->pwd) {
            session([
                'out_time' => time() + 3600 * 30,
                'admin_id' => $res->id,
                'admin_username' => $res->name
            ]);
            return JsonRespose::success(['id' => $res->id, 'id_join' => $res->id_join]);
        } else {
            return JsonRespose::fail('密码错误');
        }
    }

    /**
     * @api {post} /adminapi/system/logout 注销登录
     * @apiGroup Admin
     * @apiDescription  说明: 无
     *
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     *    "code": 1,
     *    "msg": "成功",
     *    "data": {}
     *  }
     */
    public function postLogout()
    {
        session(['out_time' => 0]);
        return JsonRespose::success();
    }
}